package example.controller;

import com.github.pagehelper.PageInfo;
import example.entity.Users;
import example.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api("会员信息")
@Controller
@RequestMapping("/admin")
public class UsersController {

    @Resource
    private UsersService usersService;
    private Object usersListPage;

    @GetMapping("/getUserById")
    public Users selectOne(Integer id){return this.usersService.queryById(id);}


    @GetMapping("/users")
    public String queryAll(Users users,
                           @RequestParam(value =  "pageNum",required = false,defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                           Model model){


        PageInfo<Users>usersPageInfo = usersService.queryAllByPage(users,pageNum,pageSize);
        model.addAttribute("usersListPage",usersPageInfo);
        return "userList";
    }

    @GetMapping("/userslist")
    public String findAll(Model model){
        List<Users>usersList = usersService.findAll();
        model.addAttribute("usersList",usersList);
        return "userList";
    }

    @PostMapping("/addUser")
    public Users addUser(Users users) {return  this.usersService.insert(users);
    }
    @PostMapping("/editUser")
    public Users editUser(Users users) {
        return this.usersService.update(users);
    }
    @GetMapping("/delUserById/{uid}")
    public String deleteById(@PathVariable("uid")Integer uid){
        this.usersService.deleteById(uid);
        return "forward:/admin/users";
    }











}
